/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.commons.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.LogFactory;

/**
 * FIXME [lko] comment me!
 * 
 * @author LKo 
 */
public class StringTools {

	/**
	 * @param value
	 *            String value to be tested.
	 * @return <strong>true</strong> if String is null or has length 0,
	 *         otherwise <strong>false</strong>.
	 */
	public static boolean isEmpty(String value) {
		return value == null || value.length() == 0;
	}

	/**
	 * @param value
	 *            String to be tested
	 * @return <strong>true</strong> if String is null or trimmed version has
	 *         length 0, othewise <strong>false</strong>.
	 */
	public static boolean isEmptyTrimmed(String value) {
		return value == null || value.trim().length() == 0;
	}

	/**
	 * Generates String containing all items of
	 * 
	 * @param values
	 *            {@link Collection} delimited by specified separator.
	 * @param values
	 *            collection of values
	 * @param separator
	 *            to be used
	 * @return string of values separated by separator
	 */
	public static String toSeparatedList(Collection<String> values, String separator) {
		Iterator<String> sit = values.iterator();
		StringBuilder bld = new StringBuilder();
		while (sit.hasNext()) {
			bld.append(sit.next());
			if (sit.hasNext()) {
				bld.append(separator);
			}
		}
		return bld.toString();
	}

	
	/**
	 * Computes SHA1 checksum of given text.
	 * 
	 * @param text	Text to be checksummed.
	 * @return	Hexa representation of checksum in String.
	 */
	public static String sha1sum(String text) {
		byte[] bytes = text.getBytes();
		try{
			MessageDigest algorithm = MessageDigest.getInstance("SHA1");
			algorithm.reset();
			algorithm.update(bytes);
			byte[] messageDigest = algorithm.digest();
			
			return convertToHex(messageDigest);
		} catch(NoSuchAlgorithmException e){
			LogFactory.getLog(StringTools.class).error("Can't create SHA1 checksum.", e);
			return "-sha1-error-rnd-" + (int)(Math.random() * 10000);
		}
	}
	
	/**
	 * Converts digest bytes to hex string.
	 * inspired by: http://www.anyexample.com/programming/java/java_simple_class_to_compute_md5_hash.xml
	 * 
	 * @param data	Byte array to be converted
	 * @return	String with hexa representation.
	 */
	private static String convertToHex(byte[] data) {
		StringBuilder bld = new StringBuilder();
		for (int i = 0; i < data.length; i++) {
			int halfbyte = (data[i] >>> 4) & 0x0F;
			int two_halfs = 0;
			do {
				if ((0 <= halfbyte) && (halfbyte <= 9)) {
					bld.append((char) ('0' + halfbyte));
				} else {
					bld.append((char) ('a' + (halfbyte - 10)));
				}
				halfbyte = data[i] & 0x0F;
			} while(two_halfs++ < 1);
		}
		return bld.toString();
	}
}
